package com.hmsc.api;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hmall.common.api.ItemClient;
import com.hmsc.api.service.ItemSearchService;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.suggest.Suggest;
import org.elasticsearch.search.suggest.SuggestBuilder;
import org.elasticsearch.search.suggest.SuggestBuilders;
import org.elasticsearch.search.suggest.completion.CompletionSuggestion;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.CollectionUtils;

import com.hmall.common.dto.ItemDTO;
import com.hmall.common.dto.PageDTO;

import java.io.IOException;
import java.util.*;

@Slf4j
@SpringBootTest
@RunWith(SpringRunner.class)
public class Texts {

    @Autowired
    private ItemClient itemClient;
    @Autowired
    private ItemSearchService itemSearchSerivce;


    /**
     * 把数据导入Es
     */
    @Test
    public void importes(){

        int page=1,size=1000;
        while (true){
            //        分页查询商品信息
            PageDTO<ItemDTO> pageDTO = itemClient.getshop(page, size);
            if(pageDTO == null || CollectionUtils.isEmpty(pageDTO.getList())){
                break;
            }
            log.info("page={}",page);
            List<ItemDTO> list = pageDTO.getList();
            ObjectMapper mapper = new ObjectMapper();
            List<ItemDTO> pojos = mapper.convertValue(list, new TypeReference<List<ItemDTO>>() { });
            // 导入Es
            itemSearchSerivce.importshop(pojos);

            if(list.size()<size){
                break;
            }
            log.info("=========================");
            page++;
        }

    }


    @Test
    public void auto() throws IOException {
       RestHighLevelClient client =
                new RestHighLevelClient(RestClient.builder(
                        HttpHost.create("http://192.168.138.100:9200")));
        SearchRequest request = new SearchRequest("shop");
//        request.source().query(QueryBuilders.matchQuery("category","拉"));
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
        boolQuery.must(QueryBuilders.matchAllQuery());
        boolQuery.filter(QueryBuilders.termQuery("brand","美旅箱包"));
        boolQuery.filter(QueryBuilders.termQuery("category","拉杆箱"));
        request.source().query(boolQuery);
        SearchResponse search = client.search(request, RequestOptions.DEFAULT);


        SearchHits hits = search.getHits();
        Map<String,List<String>> map=new HashMap<>();
        Set<String> strings=new HashSet<>();
        for (SearchHit hit : hits) {
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            String brand = sourceAsMap.get("brand").toString();
//            System.out.println(brand);
            strings.add(brand);
            for (String string : strings) {
                System.err.println(string);
            }
        }
        List<String> list=new ArrayList<>();
        BeanUtils.copyProperties(strings,list);
        map.put("分类",list);

    }

    }

